//
//  CLImageCollectionCell.m
//  CLImagePicker
//
//  Created by Criss on 2017/3/22.
//  Copyright © 2017年 Criss. All rights reserved.
//

#import "CLImageCollectionCell.h"

@implementation CLImageCollectionCell {
    UIImageView *_imageView;
    UIButton *_imageSelectedBtn;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    return self;
}

- (void)createUI {
    _imageView = [[UIImageView alloc] initWithFrame:self.contentView.frame];
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    _imageView.layer.masksToBounds = YES;
    [self.contentView addSubview:_imageView];
    
    _imageSelectedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_imageSelectedBtn addTarget:self action:@selector(imageSelectedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_imageSelectedBtn setImage:obtainImage(@"ico_check_nomal") forState:UIControlStateNormal];
    [_imageSelectedBtn setImage:obtainImage(@"ico_check_select") forState:UIControlStateSelected];
    [_imageSelectedBtn sizeToFit];
    [self.contentView addSubview:_imageSelectedBtn];
    
    CGFloat distance = 10.f;
    CGRect rect = _imageSelectedBtn.bounds;
    rect.origin.x = self.contentView.bounds.size.width - _imageSelectedBtn.bounds.size.width - distance;
    rect.origin.y = distance;
    _imageSelectedBtn.frame = rect;
    
}

-  (void)setImage:(UIImage *)image {
    _image = image;
    _imageView.image = image;
}

- (UIImageView *)obtainImageView {
    return _imageView;
}

- (void)imageSelectedBtnClick:(UIButton *)sender {
    _imageSelectedBtn.selected = !_imageSelectedBtn.isSelected;
    if (self.imageSelectedBlock) {
        self.imageSelectedBlock(_imageSelectedBtn.selected? _imageView.image: nil, self);
    }
}

- (void)setImageSelected:(BOOL)imageSelected {
    _imageSelected = imageSelected;
    _imageSelectedBtn.selected = imageSelected;
}


@end
